<!-- 门店补货选择商品 -->
<template>
	<view>
		<u-sticky >
		{{this.orderOutputNumber}}
			<u-search placeholder="商品编码11" :clearabled="true" v-model="goodsNumber" border-color="#606266"  ></u-search>
			
		</u-sticky>
		
		 
		<u-card padding="16" v-for="(item,index) in dataList" :key="index" >
			<view class="" slot="head">
				<view class="wrap">
					
					<u-row>
						<u-col span="10">
							<u-row>
								<u-col span="9" text-align="left">
									<view class="weikaishi6">
										<span>{{item.goodsName}}</span>
									</view>
								</u-col>
								<u-col span="3" text-align="right">
									<view class="weikaishi6">
										<span style="color: red;">
											<u-icon name="rmb" size="26" labelSize="13px" labelColor="#000000" ></u-icon>
										{{Number(item.goodsPrice) * Number(item.psNum) }} 
										</span>
									</view>
								</u-col>
							</u-row>
							<u-row>
								<u-col span="12" text-align="left">
									<view class="weikaishi6">
										<span>规格{{item.goodsSpecs}}</span>
									</view>
								</u-col>
								
							</u-row>
							
							
							<u-row>
								<u-col span="12" text-align="left">
									<view class="weikaishi6">
										<span>库存{{item.kcNum}}</span>
									</view>
								</u-col>
								
							</u-row>
							
							<u-row>
								<u-col span="8" text-align="left">
									<view class="weikaishi6">
										<span style="color: crimson;">
											<u-icon name="red-packet" size="26" labelSize="13px" labelColor="#crimson" label="单价"></u-icon>：{{item.goodsPrice}}/{{item.psUnit}}
											</span>
									</view>
								</u-col>
								<u-col span="4" text-align="right">
									<view class="weikaishi6">
										<u-number-box :min="1"  v-model="item.psNum" @change="valChange"></u-number-box>
									</view>
								</u-col>
							</u-row>
							
						</u-col>
						<u-col span="2" text-align="right" >
							<u-icon v-if="!item.isShoppingCart  && item.psNum >0" style="color:coral;" name="shopping-cart" size="45" labelSize="20px" labelColor="#crimson" label=""   @click="addShoop(item)"></u-icon>
							<u-icon v-if="item.isShoppingCart && item.psNum >0"style="color:coral;" name="shopping-cart-fill" size="45" labelSize="20px" labelColor="#crimson" label="" @click="addShoop(item)"></u-icon>
						</u-col>
					</u-row>
					
				</view>
			</view>
			
		</u-card>
		<view>
			<u-calendar v-if="false"  v-model="show" mode="date" max-date="2070-01-01" @change="change" ></u-calendar>
		</view>
		
		<view>
			<u-toast ref="uToast" />
		</view>
		
	</view>
</template>
<script>

	export default {
		data() {
			return {
				setMentiontime:'',
				dataList: [],
				dataList2: [],
				show:false,
				popupShow:false,
				areanumberlist:[],
				goodsNumber:'',
				pageNo:1,
				hasMore: true,
				orderOutputNumber:'',
				list2: [{
							name: '关注'
						}, {
							name: '推荐',
							badge: {
								isDot: true
							}
						}, {
							name: '电影',
							badge: {
								value: 5,
							}
						}, {
							name: '科技'
						}, {
							name: '音乐'
						}, {
							name: '美食'
						}, {
							name: '文化'
						}, {
							name: '财经'
						}, {
							name: '手工'
						}]

			}
		},
		onLoad(open) {
			console.log(open.orderOutputNumber)
			//#ifdef H5 || APP-PLUS
			this.flex = 'u-flex'
			//#endif
			
			this.orderOutputNumber = open.orderOutputNumber
			this.refresh()
			
		},
		filters:{
			formatDate(value) {
			      if (!value) return ''; // 如果没有值，返回空字符串
			      const date = new Date(value); // 将日期字符串转换为 Date 对象
			      const year = date.getFullYear(); // 获取年份
			      const month = String(date.getMonth() + 1).padStart(2, '0'); // 获取月份，+1 是因为月份从 0 开始
			      const day = String(date.getDate()).padStart(2, '0'); // 获取日期
			      return `${year}-${month}-${day}`; // 返回格式化的日期字符串
			    }

		},
		onShow() {
			// this.refresh()
		},
		onPullDownRefresh() {
			this.refresh()
			uni.stopPullDownRefresh()
		},
		
		onPullDownRefresh() {
			this.refresh()
			uni.stopPullDownRefresh()
		},
		onReachBottom() {
			uni.showLoading({
				title: '加载中'
			})
			if (this.hasMore) {
				this.pageNo++ 
				setTimeout(() => {
					this.getAllGoodsList()
					uni.hideLoading()
				}, 500)
			} else {
				setTimeout(() => {
					uni.hideLoading()
					uni.showToast({
						icon: 'none',
						title: '已经到低了哦',
						duration: 2000
					});
				}, 500)
			}
		},
		
		methods: {
			//补货
			// toBh(item){
			// 	console.log(item)
			// 	this.$Router.push({
			// 		name: 'replenishGoods',
			// 		params: {
			// 			id: item.id
			// 		},
			// 	});
			// },
			valChange(e) {
				console.log(e.value)
			},
			addShoop(item){
				console.log(item.isShoppingCart)
				
				if( true ==  item.isShoppingCart){
						this.$http.post('/rest/mdbh/deleteGoodsItem',{
						},{
							params: {
								orderOutNumber: this.orderOutputNumber,
								goodsId:item.goodsId
							},
							header: {
								'Authorization' :uni.getStorageSync('token'),
							}
						}).then(res => {
							if(res.data.success){
								item.isShoppingCart = false
								this.$refs.uToast.show({
									title: '取消商品成功',
									type: 'warning'
								})
							}
						})
				}else{
					this.$http.post('/rest/mdbh/addGoodsItem',{
					},{
						params: {
							orderOutNumber: this.orderOutputNumber,
							goodsId:item.goodsId,
							goodsName:item.goodsName,
							goodsNumber:item.goodsNumber,
							goodsPcode69:item.goodsPcode69,
							goodsPrice:item.goodsPrice,
							psNum:item.psNum,
							outMoney:item.goodsPrice * item.psNum
						},
						header: {
							'Authorization' :uni.getStorageSync('token'),
						}
					}).then(res => {
						console.log(res)
						console.log(res.data.success)
						if(res.data.success){
							item.isShoppingCart = true
							this.$refs.uToast.show({
								title: '添加商品成功',
								type: 'success'
							})
						}else{
							item.isShoppingCart = false
							this.$refs.uToast.show({
								title: '订单中已存在此商品无需添加',
								type: 'error'
							})
						}
					})
				}
				
			},
			clickCard(item){
				console.log(item)
				this.$Router.replace({
					name: 'mdBhDetails',
					params: item,
				});
			},
			getBHByGoodsNumber(value){
				 console.log(123)
				 console.log(value)
				 let dataList1 = [];
				for(let item of this.dataList) {
					if(item.goodsNumber.includes(value)){
						dataList1.push(item)
					}
				}
				this.dataList = dataList1
				if(value == null || value == '' ){
					this.refresh()
				}
			},
			checkboxChange(e){
				console.log(e)
			},
			refresh(){
				this.pageNo = 1
				this.hasMore = true
				this.dataList = [];
				uni.showLoading({
					title: '加载中...'
				})
				this.getAllGoodsList()
				uni.hideLoading()
			},
			
			getAllGoodsList(){
				this.$http.post('/rest/mdbh/allGoodsList',{
				},{
					params: {
						orderOutputNumber: this.orderOutputNumber,
						page: this.pageNo,
						limit: 5
					},
					header: {
						'Authorization' :uni.getStorageSync('token'),
					}
				}).then(res => {
					console.log(res)
					if(res.data.count == 0){
						this.$refs.uToast.show({
							title: '没有可用库存商品',
							type: 'error'
						})
					}
					if (this.pageNo == 1) {
						this.dataList = res.data.data
					} else {
						this.dataList = this.dataList.concat(res.data.data)
					}
					// this.orderCount = res.data.count
					this.hasMore = this.pageNo * 5 < res.data.count
					
				})
			},
		},
	}
</script>
<style lang="scss">
	.huanhang{
		word-break:break-all;
	}
</style>